package com.telit.firecommand.controller;

import cn.hutool.core.util.StrUtil;
import com.telit.common.entity.DataReturn;
import com.telit.common.entity.PageData;
import com.telit.common.enums.ResultCode;
import com.telit.firecommand.dto.*;
import com.telit.firecommand.entity.CommFireAlarm;
import com.telit.firecommand.service.FireQueryService;
import com.telit.firecommand.vo.ResourceSimpLevelMinVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * @author zhouyu
 * @description 火情查询接口
 */
@RestController
@RequestMapping("/fire-query")
@Api(tags = "火情查询接口", description = "/fire-query")
public class FireQueryController {

    @Autowired
    private FireQueryService fireQueryService;

    @GetMapping("/queryDetailById")
    @ApiOperation(value = "/根据火情id查询火情明细(每种状态的火情信息查询)", response = FireDetailDTO.class)
    public DataReturn queryDetailById(@RequestParam Long fireId) {
        FireDetailDTO fireDetailDTO = fireQueryService.queryDetailById(fireId);
        if (fireDetailDTO != null) {
            return DataReturn.ok(fireDetailDTO);
        } else {
            return DataReturn.ok();
        }
    }

    @GetMapping("/queryById")
    @ApiOperation("/根据火情id查询火情基本信息(火情基本信息查询)")
    public DataReturn queryById(@RequestParam Long fireId) {
        CommFireAlarm commFireAlarm = fireQueryService.queryById(fireId);
        if (commFireAlarm != null) {
            return DataReturn.ok(commFireAlarm);
        } else {
            return DataReturn.ok();
        }
    }

    @GetMapping("/querySendFireById")
    @ApiOperation("/根据火情id查询推送火情")
    public DataReturn querySendFireById(@RequestParam Long fireId) {
        CommFireAlarm commFireAlarm = fireQueryService.querySendFireById(fireId);
        if (commFireAlarm != null) {
            return DataReturn.ok(commFireAlarm);
        } else {
            return DataReturn.ok();
        }
    }


    @GetMapping("/queryTelephoneAlarm")
    @ApiOperation("/查询电话接警")
    public DataReturn queryTelephoneAlarm(@RequestParam(required = false) String districtCode) {
        //PageData<Object> pageData = fireQueryService.queryTelephoneAlarm();
        // 2022-07-05 feature by tracine：新增数据权限过滤需求

        PageData<Object> pageData = fireQueryService.queryTelephoneAlarmByDistrictCode(districtCode);
        return DataReturn.ok(pageData);
    }

    @GetMapping("/queryAllTelephoneAlarm")
    @ApiOperation("/条件查询所有电话接警")
    public DataReturn queryAllTelephoneAlarm(@RequestParam(required = false) String startTime,
                                             @RequestParam(required = false) String endTime,
                                             @RequestParam(required = true) Integer page,
                                              @RequestParam(required = true) Integer rows,
                                              @RequestParam(required = true) String  districtCode

                                             ) {
        PageData<Object> pageData = fireQueryService.queryAllTelephoneAlarm(startTime, endTime, page, rows,districtCode);
        return DataReturn.ok(pageData);
    }

    @GetMapping("/queryLeftFireInfo")
    @ApiOperation("/首页左下角查询火情信息")
    public DataReturn queryLeftFireInfo(@RequestParam(required = false) String districtCode) {
        Object list = fireQueryService.queryLeftFireInfo(districtCode);
        return DataReturn.ok(list);
    }

    @GetMapping("/queryAllStatusNumber")
    @ApiOperation("/查询所有状态火情数量")
    public DataReturn queryAllStatusNumber(@RequestParam(required = false) String districtCode) {
        Map<Object, Object> map = fireQueryService.queryAllStatusNumber(districtCode);
        return DataReturn.ok(map);
    }

    @GetMapping("/queryCount")
    @ApiOperation("/查询数量")
    public DataReturn queryCount(@RequestParam String districtCode) {
        Map<String, Object> map = fireQueryService.queryCount(districtCode);
        return DataReturn.ok(map);
    }

    @GetMapping("/queryAllSource")
    @ApiOperation("/查询所有火情来源")
    public DataReturn queryAllSource() {
        List<Map<Object, Object>> list = fireQueryService.queryAllSource();
        return DataReturn.ok(list);
    }


    @GetMapping("/queryIncompleteDisposalFire")
    @ApiOperation("/查询未完成处置火情")
    public DataReturn queryIncompleteDisposalFire(@RequestParam(required = false) Long fireId,@RequestParam(required = false) String districtCode) {
        List<CommFireAlarm> list = fireQueryService.queryIncompleteDisposalFire(fireId,districtCode);
        return DataReturn.ok(list);
    }

    @GetMapping("/queryIncompleteDisposalFireByPoint")
    @ApiOperation("/通过点位查询未完成处置火情")
    public DataReturn queryIncompleteDisposalFireByPoint(@RequestParam String firePoint) {
        List<CommFireAlarm> list = fireQueryService.queryIncompleteDisposalFireByPoint(firePoint);
        return DataReturn.ok(list);
    }

    /**
     * 条件查询所有火情,默认查询全部（增加了数据权限的部分）
     * @param status
     * @param startTime
     * @param endTime
     * @param districtCode
     * @param resource
     * @param action
     * @param finalLevel
     * @param pageNum
     * @param pageSize
     * @param flag 该值非空代表处于指挥调度模块
     * @return
     */
    @GetMapping("/queryAllFireByCondition")
    @ApiOperation("/条件查询所有火情,默认查询全部")
    public DataReturn queryAllFireByCondition(@RequestParam(required = false) String status,
        @RequestParam(required = false) String startTime,
        @RequestParam(required = false) String endTime,
        @RequestParam(required = false) String districtCode,
        @RequestParam(required = false) String resource,
        @RequestParam(required = false) String action,
        @RequestParam(required = false) String finalLevel,
        @RequestParam(required = false) Integer pageNum,
        @RequestParam(required = false) Integer pageSize,
        @RequestParam(required = false) String flag) {

        Object object = fireQueryService.queryAllFireByCondition(status, startTime, endTime, districtCode, resource, action, finalLevel, pageNum, pageSize);
        return DataReturn.ok(object);
    }

    @GetMapping("/queryAllFireForAppByCondition")
    @ApiOperation("/app端调用条件查询所有火情,默认查询全部")
    public DataReturn queryAllFireForAppByCondition(@RequestParam(required = false) String status,
        @RequestParam(required = false) String startTime,
        @RequestParam(required = false) String endTime,
        @RequestParam(required = false) String districtCode,
        @RequestParam(required = false) String resource,
        @RequestParam(required = false) String action,
        @RequestParam(required = false) String finalLevel,
        @RequestParam(required = false) Integer page,
        @RequestParam(required = false) Integer rows) {
        if(StrUtil.isBlank(districtCode)){
            districtCode = "JwtTokenUtils.getVillageDistrictCode()";
        }
        Object object = fireQueryService.queryAllFireForAppByCondition(status, startTime, endTime, districtCode, resource, action, finalLevel, page, rows);
        return DataReturn.ok(object);
    }

    @GetMapping("/appQueryAllFireByCondition")
    @ApiOperation("/app端调用 条件查询所有火情,默认查询全部")
    public DataReturn appQueryAllFireByCondition(@RequestParam(required = false) String startTime,
        @RequestParam(required = false) String endTime,
        @RequestParam(required = false) Integer page,
        @RequestParam(required = false) Integer rows) {
        Object object = fireQueryService.appQueryAllFireByCondition(startTime, endTime, page, rows);
        return DataReturn.ok(object);
    }

    @GetMapping("/queryAllFireStatusCount")
    @ApiOperation("/app端调用 查询所有状态火情数量")
    public DataReturn queryAllFireStatusCount(@RequestParam(required = false) String districtCode) {

        Map map = fireQueryService.queryAllFireStatusCount(districtCode);
        return DataReturn.ok(map);
    }


    @GetMapping("/queryAllHistoryFire")
    @ApiOperation("/条件查询所有历史火情,默认查询全部")
    public DataReturn queryAllHistoryFire(@RequestParam(required = false) String startTime,
                                          @RequestParam(required = false) String endTime,
                                          @RequestParam(required = false) String districtCode,
                                          @RequestParam(required = false) String fireLevel,
                                          @RequestParam(required = false) String resource,
                                          @RequestParam(required = false) Integer page,
                                          @RequestParam(required = false) Integer rows) {
        Object object = fireQueryService.queryAllHistoryFire(startTime, endTime, districtCode, fireLevel, resource,
                page, rows);
        return DataReturn.ok(object);
    }

    @GetMapping("/queryNotFinishFireByLongitudeAndLatitude")
    @ApiOperation("/根据经纬度查询未处置完成火情")
    public DataReturn queryNotFinishFireByLongitudeAndLatitude(@RequestParam String longitude, @RequestParam String latitude, @RequestParam(required = true) String districtCode) {
        List<CommFireAlarm> list = fireQueryService.queryNotFinishFireByLongitudeAndLatitude(longitude, latitude,districtCode);
        return DataReturn.ok(list);
    }

    @GetMapping("/queryCountHistoryFireByYear")
    @ApiOperation("/根据年份查询历史火情统计")
    public DataReturn queryCountHistoryFireByYear(@RequestParam(required = false) String year,
                                                  @RequestParam() String districtCode,
                                                  @RequestParam(required = false) String startTime,
                                                  @RequestParam(required = false) String endTime,
                                                  @RequestParam(required = false) String fireLevel,
                                                  @RequestParam(required = false) String resource) {
        HistoryFireCountDTO list = fireQueryService.queryCountHistoryFireByYear(year, districtCode, startTime, endTime,
                fireLevel, resource);
        return DataReturn.ok(list);
    }

    @GetMapping("/queryHisFireDetailById")
    @ApiOperation("/查询历史火情明细")
    public DataReturn queryHisFireDetailById(@RequestParam Long fireId) {
        HistoryFireDTO historyFireDTO = fireQueryService.queryHisFireDetailById(fireId);
        return DataReturn.ok(historyFireDTO);
    }

    @GetMapping("/queryMediaById")
    @ApiOperation("/根据火情id查询多媒体资源")
    public DataReturn queryMediaById(@RequestParam Long fireId) {
        return DataReturn.ok(fireQueryService.queryMediaById(fireId));
    }

    @GetMapping("/queryCameraByFireId")
    @ApiOperation("/根据火情id查询监控信息")
    public DataReturn queryCameraByFireId(@RequestParam Long fireId) {
        return DataReturn.ok(fireQueryService.queryCameraByFireId(fireId));
    }

    @GetMapping("/queryEmergencyPlanDetailById")
    @ApiOperation("/查询应急演练火情明细")
    public DataReturn queryEmergencyPlanDetailById(@RequestParam Long fireId) {
        EmergencyDrillDetailDTO emergencyDrillDetailDTO = fireQueryService.queryEmergencyPlanDetailById(fireId);
        return DataReturn.ok(emergencyDrillDetailDTO);
    }

    @GetMapping(value = "/queryAddressById", produces = "application/json;charset=UTF-8")
    @ApiOperation("/根据火情id查询详细地址以及行政地址")
    public DataReturn queryAddressById(@RequestParam("fireId") Long fireId) {
        return DataReturn.ok(fireQueryService.queryAddress(fireId));
    }

    @GetMapping("/queryAllHistoryEmergencyFire")
    @ApiOperation("/条件查询所有历史应急演练火情,默认查询全部")
    public DataReturn queryAllHistoryEmergencyFire(@RequestParam(required = false) String startTime,
        @RequestParam(required = false) String endTime,
        @RequestParam(required = false) String districtCode,
        @RequestParam(required = false) Integer page,
        @RequestParam(required = false) Integer rows) {
        Object object = fireQueryService.queryAllHistoryEmergencyFire(startTime, endTime, districtCode, page, rows);
        return DataReturn.ok(object);
    }

    @GetMapping("/queryCountHistoryEmergencyFireByYear")
    @ApiOperation("/根据年份查询历史应急演练火情统计")
    public DataReturn queryCountHistoryEmergencyFireByYear(@RequestParam String year) {
        List<DistrictFireCountDTO> list = fireQueryService.queryCountHistoryEmergencyFireByYear(year);
        return DataReturn.ok(list);
    }

    @GetMapping("/queryUnReadCurrentLevelFire")
    @ApiOperation("/查询本级未读火情")
    public DataReturn queryUnReadCurrentLevelFire(@RequestParam Long userId) {
        List<CommFireAlarm> commFireAlarms = fireQueryService.queryUnReadCurrentLevelFire(userId);
        return DataReturn.ok(commFireAlarms);
    }

/*    @GetMapping("/queryUnReadReportFire")
    @ApiOperation("/查询下级上报未读火情")
    public DataReturn queryUnReadReportFire(@RequestParam Long userId){
        List<CommFireAlarm> commFireAlarms = fireQueryService.queryUnReadReportFire(userId);
        return DataReturn.ok(commFireAlarms);
    }*/

    @GetMapping("/queryLongitudeAndLatitude")
    @ApiOperation("/查询火情发生经纬度")
    public DataReturn queryLongitudeAndLatitude(@RequestParam("fireId") Long fireId) {
        return DataReturn.ok(fireQueryService.queryLongitudeAndLatitude(fireId));
    }

    @GetMapping("/queryCameraFindFire")
    @ApiOperation("/查询所有监控发现火情 hasJudgment 不传查全部 0未判别 1已判别")
    public DataReturn queryCameraFindFire(@RequestParam(required = false) Integer page,
        @RequestParam(required = false) Integer rows, @RequestParam(required = false) Integer hasJudgment,
        @RequestParam(required = false) String startTime, @RequestParam(required = false) String endTime, @RequestParam(required = false) String districtCode) {
        return DataReturn.ok(fireQueryService.queryCameraFindFire(page, rows, hasJudgment, startTime, endTime, districtCode));
    }

    @GetMapping("/queryCountCameraFindFire")
    @ApiOperation("/app端调用 查询监控发现火情统计信息")
    public DataReturn queryCountCameraFindFire() {
        return DataReturn.ok(fireQueryService.queryCountCameraFindFire());
    }

    @GetMapping("/queryFireCount")
    @ApiOperation("/首页 查询当日当月当年火情统计信息 flag: day month year")
    public DataReturn queryFireCount(@RequestParam(required = false) String flag,
                                     @RequestParam(required = true) String districtCode) {
        return DataReturn.ok(fireQueryService.queryFireCount(flag,districtCode));
    }

    @GetMapping("/queryFireCountByDistrict")
    @ApiOperation("/首页 按区域查询火情统计 flag: day month year")
    public DataReturn<List<DistrictFireCountDTO>> queryFireCountByDistrict(@RequestParam(required = false) String flag,
                                                                           @RequestParam(required = true) String districtCode) {
        return DataReturn.ok(fireQueryService.queryFireCountByDistrict(flag, districtCode));
    }

    @GetMapping("/queryFireDetailByCondition")
    @ApiOperation("/首页 条件查询火情明细 flag: day month year ")
    public DataReturn queryFireDetailByCondition(@RequestParam(required = false) String flag,
                                                 @RequestParam(required = false) String districtName,
                                                 @RequestParam(required = true) String  districtCode,
                                                 @RequestParam(required = false) String fireLevel) {
        return DataReturn.ok(fireQueryService.queryFireDetailByCondition(flag, districtName,districtCode, fireLevel));
    }

    @GetMapping("/queryHistoryFireDetailByCondition")
    @ApiOperation("/首页 条件查询历史火情明细 flag: day month year ")
    public DataReturn queryHistoryFireDetailByCondition(@RequestParam(required = false) String flag, @RequestParam(required = false) String districtName, @RequestParam(required = false) String fireLevel) {
        return DataReturn.ok(fireQueryService.queryHistoryFireDetailByCondition(flag, districtName, fireLevel));
    }

    @GetMapping("/queryAllSampleLibrary")
    @ApiOperation("/根据条件查询样本库信息")
    public DataReturn queryAllSampleLibrary(@RequestParam(required = false) String startTime, @RequestParam(required = false) String endTime, @RequestParam(required = false) String districtCode, @RequestParam(required = false) Integer judgement, @RequestParam int page, @RequestParam int rows) {
        try {
            PageData<Object> sampleLibraryDtoList = fireQueryService.queryAllSampleLibrary(startTime, endTime, districtCode, judgement, page, rows);
            return DataReturn.ok(sampleLibraryDtoList);
        } catch (Exception e) {
            return DataReturn.failure(ResultCode.SERVER_ERROR, e.getMessage());
        }

    }

    @GetMapping("/queryUnfinishedFireByDay")
    @ApiOperation("/通过时间区间查询未处置完成的火情")
    public DataReturn queryUnfinishedFireByDay(@RequestParam(required = false) String day,
                                               @RequestParam(required = false) String districtCode) {
        try {
            //除了农业农村局，其他行政区划只查询本街道的火情
            List<LonAndLatDTO> commFireAlarmList = fireQueryService.queryUnfinishedFireByDay(day,districtCode);
            return DataReturn.ok(commFireAlarmList);
        } catch (Exception e) {
            return DataReturn.failure(ResultCode.SERVER_ERROR, e.getMessage());
        }

    }

    @GetMapping("/queryTaskFeedbackInfo")
    @ApiOperation(value = "/根据火情id和护林员id查询任务反馈", response = FireDetailDTO.class)
    public DataReturn queryTaskFeedbackInfo(@RequestParam Long fireId,@RequestParam Long personId) {
        List<TaskFeedbackInfoDTO> taskFeedbackInfoDTOList = fireQueryService.queryTaskFeedbackInfo(fireId, personId);
        return DataReturn.ok(taskFeedbackInfoDTOList);
    }


    @GetMapping("/queryFireCountVO")
    @ApiOperation("05--->火点列表--->报警统计接口")
    public DataReturn<List<Map>> queryFireCountVO(@RequestParam String districtCode) {
        List<Map> result = fireQueryService.queryFireCountVO( districtCode);
        return DataReturn.ok(result);
    }


    @GetMapping("/fireSimpListByViewSearch")
    @ApiOperation("01--->地图显示--->根据范围搜索火点经纬度 适用于区县级别")
    public DataReturn<List<Map>> fireSimpListByViewSearch(FireSimpListByViewSearchDTO dto) {
        List<Map> result = fireQueryService.fireSimpListByViewSearch(dto);
        return DataReturn.ok(result);
    }


    @GetMapping("/fireListBySearchLevelMin")
    @ApiOperation("02--->地图显示---->地图最小一级显示火点接口")
    public DataReturn<List<ResourceSimpLevelMinVO>> fireListBySearchLevelMin(FireSimpListByViewSearchDTO dto) {
        List<ResourceSimpLevelMinVO> result = fireQueryService.fireListBySearchLevelMin(dto);
        return DataReturn.ok(result);
    }


    @GetMapping("/fireResourceSimpListByViewSearch")
    @ApiOperation("03--->地图显示--->地图资源(包括视频，资源等)统计数量")
    public DataReturn<List<Map>> fireResourceSimpListByViewSearch(FireSimpListByViewSearchDTO dto) {
        List<Map> result = fireQueryService.fireResourceSimpListByViewSearch(dto);
        return DataReturn.ok(result);
    }


    @GetMapping("/fireListBySearchResourceLevelMin")
    @ApiOperation("04--->地图显示---->地图最小一级显示资源(包括视频，资源等)")
    public DataReturn<List<ResourceSimpLevelMinVO>> fireListBySearchResourceLevelMin(FireSimpListByViewSearchDTO dto) {
        List<ResourceSimpLevelMinVO> result = fireQueryService.fireListBySearchResourceLevelMin(dto);
        return DataReturn.ok(result);
    }

    @GetMapping("/queryFireByPage")
    @ApiOperation("06--->分页查询预警监测查询火情列表")
    public DataReturn<PageData<List<CommFireAlarm>>> queryFireByPage(FireQueryListDTO fireQueryListDTO) {
        return DataReturn.ok(fireQueryService.queryAllFireByCondition(fireQueryListDTO));
    }

}
